package com.leap.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leap.entity.AccountCategoryView;
import com.leap.entity.Article;
import com.leap.mapper.AccountCategoryMapper;
import com.leap.service.AccountCategoryService;
import com.leap.utils.Const;
import com.leap.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class AccountCategoryServiceImpl extends ServiceImpl<AccountCategoryMapper, AccountCategoryView> implements AccountCategoryService {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    JwtUtils utils;

    //更新 用户浏览各个分类次数 的表 数据
    @Override
    public void incrementViewCount(Long accountId, int categoryId, int viewCount) {
        LambdaUpdateWrapper<AccountCategoryView> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AccountCategoryView::getAccountId, accountId)
                .eq(AccountCategoryView::getCategoryId, categoryId)
                .set(AccountCategoryView::getViewCount, viewCount);
        if(!update(updateWrapper)){
            AccountCategoryView accountCategoryView = AccountCategoryView.builder()
                    .accountId(accountId)
                    .categoryId(categoryId)
                    .viewCount(viewCount)
                    .build();
            save(accountCategoryView);
        }

    }

    //按从多到少的顺序排序用户观看分类的浏览次数 的集合数据
    @Override
    public List<AccountCategoryView> getCategoryViewByAccountId(Long accountId) {
        String key = Const.ACCOUNT_CATEGORY_VIEW + accountId;

        Map<Object, Object> map = redisTemplate.opsForHash().entries(key);
        if (map == null) {
            return null;
        }
        List<AccountCategoryView> accountCategoryViews = map.entrySet().stream().map(
                        entry ->
                                AccountCategoryView.builder()
                                        .accountId(accountId)
                                        .categoryId(Integer.parseInt(entry.getKey().toString()))
                                        .viewCount(Integer.parseInt(entry.getValue().toString()))
                                        .build()
                ).sorted((a, b) -> b.getViewCount() - a.getViewCount())
                .collect(Collectors.toList());

        return accountCategoryViews;
    }

    @Override
    public void getCategoryViewByAccountIdInMySQL(Long accountId) {
        LambdaQueryWrapper<AccountCategoryView> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccountCategoryView::getAccountId, accountId);
        List<AccountCategoryView> categoryViewList = this.list(queryWrapper);

        String key = Const.ACCOUNT_CATEGORY_VIEW + accountId;

        if (!categoryViewList.isEmpty()) {
            Map<String, Integer> map = categoryViewList.stream().collect(
                    Collectors.toMap(
                            item -> Integer.toString(item.getCategoryId()),
                            item -> item.getViewCount()
                    ));

            redisTemplate.opsForHash().putAll(key,map);
        }
    }

}
